777E - Hanoi Factory - CodeForces Solution


brute force data structures dp greedy sortings *2000

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>

#define fast ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define ll long long
#define ld long double
#define el "\n"
#define ordered_set tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update>
#define ordered_multiset tree<ll, null_type,less_equal<ll>, rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
using namespace std;
const ll N = 2e5 + 5, INF = 1e18;
const ld pi = acos(-1);
const int mod = 1e9 + 7, LOG = 20;
const ld eps = 1e-4;
int dx[] = {0, -1, 0, 1, -1, 1, -1, 1};
int dy[] = { -1, 0, 1, 0, 1, -1, -1, 1};
ll n, m, k, x, y;
struct Node {
    ll val;
} zero;
Node seg[N * 4];

Node merge(Node a, Node b) {
    Node ans;
    ans.val = max(a.val , b.val);
    return ans;
}


Node solve(int idx, int l, int r, int st, int en) {
    if (en < l || st > r) {
        return zero;
    }
    if (st >= l && en <= r) {
        return seg[idx];
    }
    int mid = (st + en) / 2;
    return merge(solve(idx * 2, l, r, st, mid), solve(idx * 2 + 1, l, r, mid + 1, en));
}

void update(int idx, int l, int r, int st, int en, ll val) {
    if (st > r || en < l) {
        return;
    }
    if (st >= l && en <= r) {
        seg[idx].val = val;
        return;
    }
    int mid = (st + en) / 2;
    update(idx * 2, l, r, st, mid, val);
    update(idx * 2 + 1, l, r, mid + 1, en, val);
    seg[idx] = merge(seg[idx * 2], seg[idx * 2 + 1]);
}
struct item
{
    int a, b, h;
    item() {

    }
    item(int a, int b, int h) : a(a), b(b), h(h) {

    }

    const bool operator<(const item &other) const {
        return make_pair(b, a) < make_pair(other.b, other.a);
    }
};
item a[N];
void dowork() {
    set<int>st;
    zero.val = 0;
    map<int, int>id;
    cin >> n;
    for (int i = 1; i <= n; i++) {
        cin >> x >> y >> m;
        a[i] = item(x, y, m);
        st.insert(x);
        st.insert(y);
    }
    for (auto j : st) {
        id[j] = id.size() + 1;
    }
    sort(a + 1, a + n + 1);
    for (int i = n; i > 0; i--) {
        //cout << a[i].a << " " << a[i].b << " " << a[i].h << el;
    }
    for (int i = 1; i <= n; i++) {
        a[i].a = id[a[i].a];
        a[i].b = id[a[i].b];
    }
    ll ans, tmp;
    for (int i = n; i > 0; i--) {
        ans = a[i].h;
        if (a[i].b - 1 >= 1) {
            ans += solve(1, 1, a[i].b - 1, 1, 2 * n).val;
        }
        tmp = solve(1, a[i].a, a[i].a, 1, 2 * n).val;
        if (ans > tmp) {
            update(1, a[i].a, a[i].a, 1, 2 * n, ans);
        }
    }
    cout << solve(1, 1, 2 * n, 1, 2 * n).val << el;;

}

signed main() {
    fast
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif
    int t = 1;
    //cin >> t;
    for (int i = 1; i <= t; i++) {
        dowork();
    }
    return 0;
}


Comments

Submit
0 Comments
More Questions

1295. Find Numbers with Even Number of Digits
1704. Determine if String Halves Are Alike
1732. Find the Highest Altitude
709. To Lower Case
1688. Count of Matches in Tournament
1684. Count the Number of Consistent Strings
1588. Sum of All Odd Length Subarrays
1662. Check If Two String Arrays are Equivalent
1832. Check if the Sentence Is Pangram
1678. Goal Parser Interpretation
1389. Create Target Array in the Given Order
1313. Decompress Run-Length Encoded List
1281. Subtract the Product and Sum of Digits of an Integer
1342. Number of Steps to Reduce a Number to Zero
1528. Shuffle String
1365. How Many Numbers Are Smaller Than the Current Number
771. Jewels and Stones
1512. Number of Good Pairs
672. Richest Customer Wealth
1470. Shuffle the Array
1431. Kids With the Greatest Number of Candies
1480. Running Sum of 1d Array
682. Baseball Game
496. Next Greater Element I
232. Implement Queue using Stacks
844. Backspace String Compare
20. Valid Parentheses
746. Min Cost Climbing Stairs
392. Is Subsequence
70. Climbing Stairs